package com.boodaba.todocarts.test.app;

import java.util.HashMap;
import org.jmock.Expectations;
import org.junit.runner.RunWith;

import static com.googlecode.instinct.expect.Expect.expect;

import com.boodaba.todocarts.app.ILabelTodosActivity;
import com.boodaba.todocarts.app.LabelTodosPresenter;
import com.boodaba.todocarts.domain.ILabelRepository;
import com.boodaba.todocarts.domain.Label;
import com.googlecode.instinct.integrate.junit4.InstinctRunner;
import com.googlecode.instinct.marker.annotate.BeforeSpecification;
import com.googlecode.instinct.marker.annotate.Mock;
import com.googlecode.instinct.marker.annotate.Specification;
import com.googlecode.instinct.marker.annotate.Subject;

@RunWith(InstinctRunner.class)
public class LabelTodosPresenterSpecs {
	@Subject private LabelTodosPresenter SUT;
	
	@Mock private ILabelTodosActivity labelTodosActivity;
	@Mock private ILabelRepository labelRepository;
	private Label targetLabel;
	
	@BeforeSpecification
	public void setUp() {
		SUT = new LabelTodosPresenter(labelRepository);
		targetLabel = new Label("some label");
		targetLabel.inviteTodo("incompleted todo 1");
		targetLabel.inviteTodo("completed todo 1");
		targetLabel.completeTodo("completed todo 1");
		targetLabel.inviteTodo("incompleted todo 2");
		targetLabel.inviteTodo("incompleted todo 3");
		targetLabel.inviteTodo("completed todo 2");
		targetLabel.completeTodo("completed todo 2");
	}
	
	@Specification
	void shouldSetActiveTodoTitleListOnActivityStart() {
		expect.that(new Expectations() {{
			oneOf(labelTodosActivity).getTargetLabelTitle(); will(returnValue("some label"));
			oneOf(labelRepository).getLabelByTitle("some label"); will(returnValue(targetLabel));
		}});
		expect.that(new Expectations() {{
						
			HashMap<String, Boolean> titleDict = new HashMap<String, Boolean>();
			titleDict.put("incompleted todo 1", false);
			titleDict.put("incompleted todo 3", false);
			titleDict.put("incompleted todo 2", false);
			
			oneOf(labelTodosActivity).setLabelTitle("some label");
			oneOf(labelTodosActivity).setTodoList(titleDict);
		}});
		SUT.initializeWith(labelTodosActivity);
	}

	@Specification
	void shouldStartSettingTodos() {
		expect.that(new Expectations() {{
			oneOf(labelTodosActivity).getTargetLabelTitle(); will(returnValue("label"));
			oneOf(labelRepository).getLabelByTitle("label"); will(returnValue(new Label("label")));
			oneOf(labelTodosActivity).setLabelTitle("label");
			oneOf(labelTodosActivity).setTodoList(new HashMap<String, Boolean>());
			
			oneOf(labelTodosActivity).startSettingTodos("label");
		}});
		SUT.initializeWith(labelTodosActivity);
		SUT.startSettingTodos();
	}
	
	@Specification
	void shouldMarkTodo() {
		expect.that(new Expectations() {{
			oneOf(labelTodosActivity).getTargetLabelTitle(); will(returnValue("some label"));
			oneOf(labelRepository).getLabelByTitle("some label"); will(returnValue(targetLabel));
			oneOf(labelTodosActivity).setLabelTitle("some label");
			HashMap<String, Boolean> titleDict = new HashMap<String, Boolean>();
			titleDict.put("incompleted todo 1", false);
			titleDict.put("incompleted todo 3", false);
			titleDict.put("incompleted todo 2", false);
			oneOf(labelTodosActivity).setTodoList(titleDict);
			
			oneOf(labelTodosActivity).getSelectedTodoTitle(); will(returnValue("incompleted todo 3"));
			oneOf(labelRepository).save(targetLabel);
			oneOf(labelTodosActivity).setTodoMarked("incompleted todo 3");
		}});
		SUT.initializeWith(labelTodosActivity);
		SUT.processTodo();
	}
	
	@Specification
	void shouldUnmarkTodo() {
		expect.that(new Expectations() {{
			oneOf(labelTodosActivity).getTargetLabelTitle(); will(returnValue("some label"));
			oneOf(labelRepository).getLabelByTitle("some label"); will(returnValue(targetLabel));
			oneOf(labelTodosActivity).setLabelTitle("some label");
			
			HashMap<String, Boolean> titleDict = new HashMap<String, Boolean>();
			titleDict.put("incompleted todo 1", false);
			titleDict.put("incompleted todo 3", false);
			titleDict.put("incompleted todo 2", false);
			
			oneOf(labelTodosActivity).setTodoList(titleDict);
			
			oneOf(labelTodosActivity).getSelectedTodoTitle(); will(returnValue("incompleted todo 3"));
			oneOf(labelRepository).save(targetLabel);
			oneOf(labelTodosActivity).setTodoMarked("incompleted todo 3");
			
			oneOf(labelTodosActivity).getSelectedTodoTitle(); will(returnValue("incompleted todo 3"));
			oneOf(labelRepository).save(targetLabel);
			oneOf(labelTodosActivity).setTodoUnmarked("incompleted todo 3");
		}});
		SUT.initializeWith(labelTodosActivity);
		SUT.processTodo();
		SUT.processTodo();
	}
}
